package com.hzh.cusviewdemo.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.hzh.cusviewdemo.Bean.MusicInfoData;
import com.hzh.cusviewdemo.Bean.MusicSearchData;
import com.hzh.cusviewdemo.Presenter.MusicResponse;
import com.hzh.cusviewdemo.R;

import java.util.List;

/**
 * 歌曲列表适配器
 */
public class MusicListAdapter extends RecyclerView.Adapter<MusicListAdapter.MyHolder> {

    private List<MusicSearchData> dataList;
    private Context context;

    private OnMusicClickListener mListener;

    public interface OnMusicClickListener {
        void onMusicClick(MusicSearchData data);
    }

    public void setOnMusicClickListener(OnMusicClickListener listener) {
        this.mListener = listener;
    }

    public MusicListAdapter(List<MusicSearchData> dataList, Context context) {
        this.dataList = dataList;
        this.context = context;
    }

    @NonNull
    @Override
    public MusicListAdapter.MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_music_list, parent, false);
        return new MyHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MusicListAdapter.MyHolder holder, int position) {
        MusicSearchData absData = dataList.get(position);
        holder.iml_song_name.setText(absData.getName());
        holder.iml_song_own.setText(absData.getArtist());
        holder.iml_linear.setOnClickListener(v -> {
            mListener.onMusicClick(absData);
        });
        holder.iml_vip_tag.setVisibility(View.VISIBLE);
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    static class MyHolder extends RecyclerView.ViewHolder {

        private LinearLayout iml_linear;
        private TextView iml_song_name;
        private TextView iml_song_own;
        private TextView iml_vip_tag;

        public MyHolder(@NonNull View itemView) {
            super(itemView);

            iml_linear = itemView.findViewById(R.id.iml_linear);
            iml_song_name = itemView.findViewById(R.id.iml_song_name);
            iml_song_own = itemView.findViewById(R.id.iml_song_own);
            iml_vip_tag = itemView.findViewById(R.id.iml_vip_tag);
        }
    }
}
